<?php
/**
 * File htdocs/Aplication/Models/updateque.php ini berisi class Updateque
 *
 * PHP version 5.3
 *
 * @category Models
 * @package  Htdocs/Aplication/Models
 * @author   dio ratar <dio.22ratar@gmail.com>
 * @license  FamilyTree
 * (http://en.wikipedia.org/wiki/Proprietary_software)
 * @link     http://www.FamilyTree.dio22r.com/
 */

if ( ! defined('BASEPATH')) exit('No direct script access allowed');


/**
 * pengisian form :
 * tambah :
 * 1. pilih mau nambah anak ato pasangan...
 * 2. pilih pasangan/anaknya siapa....
 * 3. isi form...
 * 4. upload foto... boleh di skip...
 * 5. view bio...
 * 6. close...
 * 
 *  
 *  edit :
 *  1. lansung klik di nama
 *  2. tampil view bio...
 *  3. edit or close...
 *  4. edit : isi form...
 *  5 view bio...
 */

/**
 * Class updateque merupakan class yang berisi perintah-perintah SQL 
 * yang digunakan untuk update.
 *
 * @category Models
 * @package  Htdocs/Aplication/Models
 * @author   dio ratar <dio.22ratar@gmail.com>
 * @license  FamilyTree
 * (http://en.wikipedia.org/wiki/Proprietary_software)
 * @link     http://www.FamilyTree.dio22r.com/
 */
class biodata extends CI_Controller
{
	
	public function __construct()
	{
		parent::__construct();
		
		
		$this->load->helper("html");
		$this->load->helper("form");
		$this->load->helper("url");
		$this->load->library("session");
		
		$sesDat = $this->session->userdata("id", true);
		if ($sesDat == false) {
			redirect("/home/login", "refresh");
		}
		
		$this->load->model("biodata_model", "bioModel");
		
	}
	
	public function form_edit($mode = "new", $id = "")
	{
		if ("new" == $mode) {
			$arrData = array (
				'nama' => '',
				'tempat_lahir' => '',
				'tanggal_lahir' => '',
				'jenis_kelamin' => '',
				'lokasi_skrg' => ''
			);
			$viewData = array (
				'ctlArrData' => $arrData,
				'ctlSubmitMode' => "add"
			);
		} else if ("edit" == $mode) {
			$arrRes = $this->select->bio($id);
			$arrData = array (
				'nama' => $arrRes['nama'],
				'tempat_lahir' => $arrRes['tempat_lahir'],
				'tanggal_lahir' => $arrRes['tanggal_lahir'],
				'jenis_kelamin' => $arrRes['jenis_kelamin'],
				'lokasi_skrg' => $arrRes['lokasi_skrg']
			);
			$viewData = array (
				'ctlArrData' => $arrData,
				'ctlSubmitMode' => "edit"
			);
		}
		
		
		$this->load->view('biodata/form_bio', $viewData);
	}
	
	public function form_new()
	{
		$openData = array (
			'ctlTitle' => 'testing',
			'ctlScript' => '
				<link rel="stylesheet" type="text/css"
				 href="' . base_url() . 'css/form.css" />
				<script type="text/javascript"
				 src="' . base_url() . 'javascript/jquery.js">
				</script>
				<script type="text/javascript"
				 src="' . base_url() . 'javascript/jquery.cycle.js">
				</script>
				<script type="text/javascript"
				 src="' . base_url() . 'javascript/jquery.tinyscrollbar.js">
				</script>
				<script type="text/javascript"
				 src="' . base_url() . 'javascript/form.js">
				</script>
				'
		);
		
		//$this->load->view('open', $openData);
		
		$viewData = array(
			'ctlFormAction' => base_url() . "index.php/"
				. strtolower(__CLASS__) ."/insert_data",
			'ctlDataMember' => $this->retrieve_all()
		);
		
		$this->load->view('biodata/view_form_bio_new', $viewData);
	}
	
	public function show_bio($id)
	{
		
		$arrData = $this->_retrieve_bio($id);
			$openData = array (
			'ctlTitle' => 'testing',
			'ctlScript' => '
				<link rel="stylesheet" type="text/css"
				 href="' . base_url() . 'css/form.css" />
				<script type="text/javascript"
				 src="' . base_url() . 'javascript/jquery.js">
				</script>
				<script type="text/javascript"
				 src="' . base_url() . 'javascript/jquery.cycle.js">
				</script>
				<script type="text/javascript"
				 src="' . base_url() . 'javascript/jquery.tinyscrollbar.js">
				</script>
				<script type="text/javascript"
				 src="' . base_url() . 'javascript/form.js">
				</script>
				'
		);
		
		//$this->load->view('open', $openData);
		
		$profPict = base_url() . "image/profile_pict/" . $arrData['photo'];
		
		$dir = dirname(__FILE__);
		$baseDir = $dir . "/../../";
		
		$filePath = $baseDir . "image/profile_pict/" . $arrData['photo'];
		
		if (is_file($filePath)) {
		    $arrData['photo'] = $profPict;
		} else {
		    $arrData['photo'] = base_url() . "image/profile_pict/profpict.png";
		}
		
		if ($arrData != false) {
			$arrView = array (
				'ctlArrBio' => $arrData
			);
			
			//print_r($arrData);
			$this->load->view('biodata/view_bio', $arrView);
		} 
		
	}
	
	public function _retrieve_bio($id)
	{
		$arrData = array (
			'id_data_diri' => $id 
		);
		
		$result = $this->bioModel->general_select(
			"data_diri", $arrData
		);
		
		if (count($result) > 0) {
			return $result[0];
		} else {
			return false;
		}

	}
	
	
	public function retrieve_child($id)
	{
		$arrSelect = array (
			'id_data_diri' => $id
		);
		
		$result = $this->bioModel->general_select(
			"data_diri", $arrSelect
		);
		
		$arrChild = array();
		
		if (count($result) > 0) {
			foreach ($result as $key => $arrVal) {
				$arrChild[] = $arrVal['nama'];
			}
		}
		
		return $arrChild;
	}
	
	public function retrieve_couple($id)
	{
		$arrSelect = array (
			'id_data_diri' => $id
		);
		
		$result = $this->bioModel->general_select(
			"data_diri", $arrSelect
		);
		
		$arrChild = array();
		
		if (count($result) > 0) {
			foreach ($result as $key => $arrVal) {
				$arrChild[] = $arrVal['nama'];
			}
		}
		
		return $arrChild;
	}
	
	public function retrieve_button_member()
	{
		$result = $this->bioModel->retrieve_all();
		
		foreach ($result as $key => $arrVal) {
			$readyData = array (
			    'name' => 'b_id',
			    'class' => 'next',
			    'value' => $arrVal['id_data_diri'],
			    'type' => 'button',
			    'content' => $arrVal['nama']
			);
			echo form_button($readyData);
		}
	}
	
	public function retrieve_button_couple()
	{
		$result = $this->bioModel->retrieve_couple();
		
		foreach ($result as $key => $arrVal) {
			$readyData = array (
			    'name' => 'b_id',
			    'class' => 'next',
			    'value' => $arrVal['id_nikah'],
			    'type' => 'button',
			    'content' => $arrVal['pria'] . " & "
			    	. $arrVal['wanita']
			);
			echo form_button($readyData);
		}
	}
	
	public function retrieve_all()
	{
		$result1 = $this->bioModel->retrieve_couple();
		
		$readyData = array();
		
		foreach ($result1 as $key => $arrVal) {
			$tempData = array (
			    'name' => 'b_id',
			    'class' => 'next couple',
			    'value' => $arrVal['id_nikah'],
			    'type' => 'button',
			    'content' => strtoupper($arrVal['pria'] . " & "
			    	. $arrVal['wanita'])
			);
			
			$readyData[] = $tempData;
		}
		
		$result2 = $this->bioModel->retrieve_all();
		
		foreach ($result2 as $key => $arrVal) {
			$tempData = array (
			    'name' => 'b_id',
			    'class' => 'next child',
			    'value' => $arrVal['id_data_diri'],
			    'type' => 'button',
			    'content' => strtoupper($arrVal['nama'])
			);
			
			$readyData[] = $tempData;
		}
		
		return $readyData;
	}
	
	public function validate_form()
	{
		$name = $this->input->post("nama", true);
		$poBirth = $this->input->post("tempat_lahir", true);
		$doBirth = $this->input->post("tanggal_lahir", true);
		$sex = $this->input->post("jenis_kelamin", true);
	}
	
	public function insert_data() {
		$isSubmit = $this->input->post("submit", true);
		
		if ($isSubmit) {
			$id = $this->input->post("h_id", true);
			$name = $this->input->post("nama", true);
			$poBirth = $this->input->post("tempat_lahir", true);
			$doBirth = $this->input->post("tanggal_lahir", true);
			$sex = $this->input->post("jenis_kelamin", true);
			$type = $this->input->post("h_tipe", true);	

			$this->bioModel->insert_bio(
				$id, $name, $poBirth, $doBirth, $sex, $type
			);
		}
		
	}
	
	
}